package com.service;

import com.constant.CommentStateConst;
import com.dao.OrdersDao;
import com.dto.OrdersDto;
import com.pojo.Orders;
import com.pojo.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrdersService {

    @Resource
    private OrdersDao ordersDao;

    @Value("${businessImage.url}")
    private String businessImageUrl;


    public boolean add(OrdersDto ordersDto) {
        Orders orders = new Orders();
        BeanUtils.copyProperties(ordersDto, orders);
        orders.setCommentState(CommentStateConst.NOT_COMMENT);
        ordersDao.insert(orders);
        return true;
    }


    public List<OrdersDto> getListByMemberId(Long memberId, Page page) {
        List<OrdersDto> result = new ArrayList<OrdersDto>();
        int pageNo=0;
        if (page!=null){
            pageNo = (page.getCurrentPage()-1)*page.getPageNumber();
        }
        List<Orders>  ordersList = ordersDao.select(memberId,pageNo,page.getPageNumber());
        for(Orders orders : ordersList) {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(orders, ordersDto);
            ordersDto.setImg(businessImageUrl + orders.getBusiness().getImgFileName());
            ordersDto.setTitle(orders.getBusiness().getTitle());
            ordersDto.setCount(orders.getBusiness().getNumber());
            ordersDto.setId(orders.getId());
            ordersDto.setPrice(orders.getPrice());
            ordersDto.setMember(orders.getMember());
            result.add(ordersDto);
        }
        return result;
    }

    public OrdersDto getById(Long id) {
        OrdersDto result = new OrdersDto();
        Orders orders = ordersDao.selectById(id);
        BeanUtils.copyProperties(orders, result);
        return result;
    }

}
